import scrapy

from scrapy.selector import Selector


class ListSpider(scrapy.Spider):
    name = 'list'

    start_urls = ['http://beijing.liebiao.com/jiadianweixiu/']

    # parse
    def parse(self, response):
        # print('body',response.body)

        selector = Selector(response)

        # print(selector)
        # 获取所有的节点
        services = selector.xpath("//li[@class='list-item clearfix ']")

        # 循环节点查找
        for service in services:
            term = service.xpath(
                "div[@class='list-item-body fl']/div[@class='list-item-main fl']/div[@class='list-title-wrapper']/h2[@class='list-title']/a[@class='list-title-link']/text()").extract_first()
            photo = service.xpath(
                "div[@class='list-item-body fl']/div[@class='list-cover-link fl']/img[@class='list-cover']/@src").extract_first()
            category = service.xpath(
                "div[@class='list-item-body fl']/div[@class='list-item-main fl']/div[@class='list-param'][1]/span[@class='list-param-item']/span/text()").extract()
            tag = service.xpath(
                "div[@class='list-item-body fl']/div[@class='list-item-main fl']/div[@class='list-param'][2]/span/text()").extract()
            company = service.xpath(
                "div[@class='list-item-body fl']/div[@class='list-item-main fl']/div[@class='list-param'][2]/span[@class='list-param-item']/a[@class='list-param-link']/text()").extract_first()
            details_link = service.xpath(
                "div[@class='list-item-body fl']/div[@class='list-item-main fl']/div[@class='list-param'][2]/span[@class='list-param-item']/a[@class='list-param-link']/@href").extract_first()

            phone = service.xpath(
                "div[@class='list-item-extra fr']/div[@class='list-item-extra-content']/div[@class='contact-box']/div/div[@class='contact-info']/div[@class='contact-info-content']/p[@class='contact-phone']/span/text()").extract_first()

            if phone:
                phone = self.DecodePhone(phone)

            yield {
                'term': term,
                # 'photo': photo,
                'category': category,
                'tag': tag,
                'company': company,
                # 'details_link': details_link,
                'phone': phone
            }

        # 下一页
        next_page_num = selector.xpath(
            "//div[@class='pager']/ul[@class='pagination']/li[@class='active']/span/text()").extract_first()

        last_url = selector.xpath(
            "//div[@class='pager']/ul[@class='pagination']/li[last()]/a/@href").extract_first()
            
        num = str(int(next_page_num) + 1)
        next_url = "http://beijing.liebiao.com/jiadianweixiu/index" + num + ".html"

        yield scrapy.Request(response.urljoin(next_url))

    # 电话解码

    def DecodePhone(self, params):
        phone = ''
        # 字典
        dictionary = {
            "a": 1,
            "b": 8,
            "c": 3,
            "d": 7,
            "e": 0,
            "f": 9,
            "g": 4,
            "h": 5,
            "i": 2,
            "j": 6,
            "k": "-"
        }

        for param in params:
            phone += str(dictionary[param])

        return phone
